package com.ruoyi.shop.trade.service.impl;

import java.util.Calendar;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.shop.common.constant.OrderConstants;
import com.ruoyi.shop.trade.domain.ShopOrder;
import com.ruoyi.shop.trade.mapper.ShopOrderMapper;
import com.ruoyi.shop.trade.service.IShopOrderJobService;
import com.ruoyi.shop.trade.vo.ShopOrderVo;

@Service
public class ShopOrderJobServiceImpl implements IShopOrderJobService {
	@Autowired
	private ShopOrderMapper shopOrderMapper;

	/**
	 * 支付超时自动取消订单
	 */
	@Override
	public void payOrderCancel() {
		List<ShopOrder> orderList = queryPayExpireList();// 支付超时订单列表
		orderList.forEach(order -> {
			this.cancelOrder(order);//取消订单
		});
	}

	/**
	 * 查询支付失效订单
	 * 
	 * @return
	 */
	private List<ShopOrder> queryPayExpireList() {
		Integer timeoutMinute = 120;//过期时间 120分钟
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MINUTE, -timeoutMinute);
		ShopOrderVo orderVo = new ShopOrderVo();
		orderVo.setExpireTime(cal.getTime());
		orderVo.setStatus(OrderConstants.ORDER_STATUS_PAY);
		return shopOrderMapper.queryExpireList(orderVo);
	}

	/**
	 * 取消订单(改状态)
	 * 
	 * @param order
	 */
	private void cancelOrder(ShopOrder order) {
		order.setCloseTime(DateUtils.getNowDate());
		order.setStatus(OrderConstants.ORDER_STATUS_CLOASE);
		shopOrderMapper.updateStatus(order);
	}
}
